package com.Manage;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class addStudents {
    //统一数据源
    //将学生数据存储在一个共享的静态数组中，确保所有的操作都基于同一份数据
   private static String[] arr1 = new String[0];
   private static int currentStuId = 202400100;
   //静态初始化
    public static void Date() {
        random r = new random();
        r.randomStu(); // 生成初始100个学生
        arr1 = r.getStudentNames();
    }
    //获取数据
    public static String[] getNames() {
        return arr1.clone();
    }

    public static void Add(){
        String ways;
        Scanner myscanner = new Scanner(System.in);
        do {
            System.out.println("是否添加学生 yes/no");
            ways = myscanner.next();
            if(!ways.equalsIgnoreCase("no")&&!ways.equalsIgnoreCase("yes")){
                System.out.println("输入错误，请重新输入");
                continue;//结束本次循环，直接进入下一次循环，避免程序继续进行下去，造成逻辑混乱
            }
            if(ways.equalsIgnoreCase("no")){
                break;//若用户输出no,则之间跳出循环
            }
            System.out.println("======当前的学生=======");
            for(int i=0;i<arr1.length;i++){
                System.out.println(arr1[i]);
            }
            String[] arr2 = new String[arr1.length + 1];
            for (int i = 0; i < arr1.length; i++) {
                arr2[i] = arr1[i];
            }
            System.out.println("输入你要添加的学生");
            String addStu = myscanner.next();
            currentStuId++;
            System.out.println("输入所添加学生的年龄");
            String targetage = myscanner.next();
            String newStudent = currentStuId + "：" + addStu + " age: " + targetage;
            arr2[arr2.length - 1] = newStudent;
            arr1 = arr2;//将arr2中的元素全部传给arr1，地址传递
            System.out.println("======添加后的效果======");
            System.out.println(newStudent);
            System.out.println("");//换行
            System.out.println("添加成功");
        } while (true);
        System.out.println("=====添加学生进程结束=====");
        myscanner.close();
    }

}

